package com.railway.switchs.action.business;

import com.railway.switchs.aspect.OperationLog;
import com.railway.switchs.base.BaseResponse;
import com.railway.switchs.constant.SystemConstant;
import com.railway.switchs.domain.dto.business.SysNews;
import com.railway.switchs.domain.query.business.SysNewsQuery;
import com.railway.switchs.enums.EnumDateStyle;
import com.railway.switchs.enums.EnumNewsType;
import com.railway.switchs.enums.EnumReturnResult;
import com.railway.switchs.service.business.ISysNewsService;
import com.railway.switchs.utils.DateUtil;
import com.railway.switchs.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @ClassName SysNewsAction
 * @Author 魏梓晟
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/news")
public class SysNewsAction {

    @Autowired
    private ISysNewsService sysNewsService;

    /**
     * 进入新闻列表页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return "news/list";
    }

    /**
     * 获取新闻数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public SysNewsQuery getList(SysNewsQuery query) {
        try {
            query = sysNewsService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取新闻列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取新闻数据异常");
        }
        return query;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String preAdd(){
        return "news/add";
    }

    /**
     * 新增新闻
     * @param sysNews
     * @param result
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="A",actionType ="0" )
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated SysNews sysNews, BindingResult result) {
        BaseResponse response = new BaseResponse();
        try {
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("SysNews-add参数有误");
                log.info("SysNews-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            sysNews.setNewsType(EnumNewsType.TO_BE_ANNOUNCED.getCode());
            sysNews.setEndTime(DateUtil.StringToDate(sysNews.getEndTimeStr(), EnumDateStyle.YYYY_MM_DD_HH_MM_SS));
            sysNewsService.add(sysNews);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("新增新闻成功");
        } catch (Exception e) {
            log.error("新增新闻异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("新增新闻失败");
        }
        return response;
    }

    /**
     * 进入新闻信息修改页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView preEdit(Long id){
        ModelAndView mav = new ModelAndView();
        try{
            SysNews sysNews = sysNewsService.get(id);
            mav.addObject("sysNews",sysNews);
            mav.setViewName("news/edit");
        }catch (Exception e){
            log.error("获取新闻详情失败",e);
            mav.addObject(SystemConstant.ERROR_KEY,"系统异常");
            mav.setViewName("news/list");
        }
        return mav;
    }

    /**
     * 修改新闻
     * @param sysNews
     * @param result
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="A",actionType ="1" )
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public BaseResponse edit(@Validated SysNews sysNews, BindingResult result) {
        BaseResponse response = new BaseResponse();
        try {
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("SysNews-edit参数有误");
                log.info("SysNews-edit参数有误："+result.getAllErrors().toString());
                return response;
            }
            sysNews.setEndTime(DateUtil.StringToDate(sysNews.getEndTimeStr(), EnumDateStyle.YYYY_MM_DD_HH_MM_SS));
            sysNewsService.edit(sysNews);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("修改新闻成功");
        } catch (Exception e) {
            log.error("修改新闻异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("修改新闻失败");
        }
        return response;
    }

    /**
     * 删除新闻
     * @param id
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="A",actionType ="2" )
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public BaseResponse delete(Long id) {
        BaseResponse response = new BaseResponse();
        try {
            sysNewsService.delete(id);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("删除新闻成功");
        } catch (Exception e) {
            log.error("删除新闻异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("删除新闻失败");
        }
        return response;
    }

    /**
     * 发布新闻
     * @param id
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="A",actionType ="5" )
    @RequestMapping(value = "/up",method = RequestMethod.POST)
    public BaseResponse up(Long id) {
        BaseResponse response = new BaseResponse();
        try {
            SysNews sysNews = sysNewsService.get(id);
            if(sysNews.getEndTime().before(new Date())){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("新闻公示截止时间需在当前时间之后");
            }
            sysNews.setNewsType(EnumNewsType.HAS_ANNOUNCED.getCode());
            sysNewsService.edit(sysNews);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("发布新闻成功");
        } catch (Exception e) {
            log.error("发布新闻异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("发布新闻失败");
        }
        return response;
    }

    /**
     * 撤下新闻
     * @param id
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="A",actionType ="6" )
    @RequestMapping(value = "/down",method = RequestMethod.POST)
    public BaseResponse down(Long id) {
        BaseResponse response = new BaseResponse();
        try {
            SysNews sysNews = sysNewsService.get(id);
            sysNews.setNewsType(EnumNewsType.HAS_RESCINDED.getCode());
            sysNewsService.edit(sysNews);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("撤下新闻成功");
        } catch (Exception e) {
            log.error("撤下新闻异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("撤下新闻失败");
        }
        return response;
    }

    /**
     * 新闻预览
     * @param id
     * @return
     */
    @RequestMapping(value = "/preview",method = RequestMethod.GET)
    public ModelAndView preview(Long id) {
        ModelAndView mav = new ModelAndView();
        try{
            SysNews sysNews = sysNewsService.get(id);
            mav.addObject("sysNews",sysNews);
        }catch (Exception e){
            log.error("新闻预览异常",e);
            mav.addObject(SystemConstant.ERROR_KEY, "新闻预览异常");
        }
        mav.setViewName("news/preview");
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/getAllList",method = RequestMethod.POST)
    public SysNewsQuery getAllList(SysNewsQuery query) {
        try {
            List<SysNews> sysNewsList = sysNewsService.getList(query);
            query.setData(sysNewsList);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取新闻数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取新闻异常");
        }
        return query;
    }
}
